package com.lenovo.srl.dil.tiltbase.lib;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.lenovo.srl.dil.tiltbase.lib.fragment.BaseFragment;

public abstract class BaseActivity extends FragmentActivity {
	BaseFragment fragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		fragment = getMyFragment();
		getSupportFragmentManager().beginTransaction()
				.add(R.id.container, fragment).commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST, 1, "设置");
		menu.add(Menu.NONE, Menu.FIRST + 1, 2, "错了，重置");
		menu.add(Menu.NONE, Menu.FIRST + 2, 3, "实验开始");
		menu.add(Menu.NONE, Menu.FIRST + 3, 4, "实验结束");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == Menu.FIRST) {
			Intent intent = new Intent(this, BaseApp.getInstance()
					.getSettingActivityClazz());
			intent.putExtra("mode", fragment.getCtrlMode().ordinal());
			startActivity(intent);
			return true;
		} else if (item.getItemId() == Menu.FIRST + 1) {
			fragment.reset();
			return true;
		} else if (item.getItemId() == Menu.FIRST + 2) {
			fragment.begin();
			return true;
		} else if (item.getItemId() == Menu.FIRST + 3) {
			fragment.end();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public abstract BaseFragment getMyFragment();
}
